using com.google.zxing;
using com.google.zxing.common;
using com.google.zxing.qrcode;
using MicroMsg.UI.Util;
using Microsoft.Devices;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace MicroMsg.UI.Page
{
	public class ScanQRCode : BasePage
	{
		private DispatcherTimer _timer;
		private ObservableCollection<string> _matches;
		private PhotoCameraLuminanceSource _luminance;
		private QRCodeReader _reader;
		private PhotoCamera _photoCamera;
		internal Grid LayoutRoot;
		internal Rectangle _previewRect;
		internal VideoBrush _previewVideo;
		internal CompositeTransform _previewTransform;
		internal TextBlock txtShow;
		internal ListBox _matchesList;
		private bool _contentLoaded;
		public ScanQRCode()
		{
			this.InitializeComponent();
			this._matches = new ObservableCollection<string>();
			this._matchesList.set_ItemsSource(this._matches);
			this._timer = new DispatcherTimer();
			this._timer.set_Interval(TimeSpan.FromMilliseconds(250.0));
			DispatcherTimer arg_62_0 = this._timer;
			EventHandler eventHandler = delegate(object sender, EventArgs e)
			{
				this.ScanPreviewBuffer();
			};
			arg_62_0.add_Tick(eventHandler);
		}
		protected override void OnPageIn(object[] object_0)
		{
			this._photoCamera = new PhotoCamera();
			this._photoCamera.add_Initialized(new EventHandler<CameraOperationCompletedEventArgs>(this.OnPhotoCameraInitialized));
			CameraVideoBrushExtensions.SetSource(this._previewVideo, this._photoCamera);
			CameraButtons.add_ShutterKeyHalfPressed(new EventHandler(this.CameraButtons_ShutterKeyHalfPressed));
		}
		private void CameraButtons_ShutterKeyHalfPressed(object sender, EventArgs e)
		{
			if (this._photoCamera != null)
			{
				this._photoCamera.Focus();
			}
		}
		protected override void OnPageOut()
		{
			this._luminance = null;
			this._photoCamera = null;
			this._previewVideo = null;
			this._timer.Stop();
			this._timer = null;
			this._reader = null;
			this._matches = null;
		}
		private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e)
		{
			int width = Convert.ToInt32(this._photoCamera.get_PreviewResolution().get_Width());
			int height = Convert.ToInt32(this._photoCamera.get_PreviewResolution().get_Height());
			this._luminance = new PhotoCameraLuminanceSource(width, height);
			this._reader = new QRCodeReader();
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this._previewTransform.set_Rotation(this._photoCamera.get_Orientation());
				this._timer.Start();
			});
		}
		private void ScanPreviewBuffer()
		{
			try
			{
				this._photoCamera.GetPreviewBufferY(this._luminance.PreviewBufferY);
				HybridBinarizer hybridBinarizer = new HybridBinarizer(this._luminance);
				BinaryBitmap binaryBitmap = new BinaryBitmap(hybridBinarizer);
				Result result = this._reader.decode(binaryBitmap);
				base.get_Dispatcher().BeginInvoke(delegate
				{
					this.DisplayResult(result.get_Text());
				});
			}
			catch
			{
			}
		}
		private void DisplayResult(string text)
		{
			if (text == null)
			{
				return;
			}
			if (this.txtShow.get_Text() != text)
			{
				this.txtShow.set_Text(text);
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/BarCodeTool/ScanQRCode.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this._previewRect = (Rectangle)base.FindName("_previewRect");
			this._previewVideo = (VideoBrush)base.FindName("_previewVideo");
			this._previewTransform = (CompositeTransform)base.FindName("_previewTransform");
			this.txtShow = (TextBlock)base.FindName("txtShow");
			this._matchesList = (ListBox)base.FindName("_matchesList");
		}
	}
}
